// packageA/member/shared_partner/sharedPartner.js
import urilt from "../../utils/util";
const app = getApp();
var date = new Date();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    member: {
      nickname: "",
      member_id: "",
      level_name: "",
      validity_value: "",
      src: 'https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&fmt=auto&app=138&f=JPEG?w=632&h=500',
      cumulative_share_amoun: "",
      last_period: "",
      frequency: '',
    },
    active: '',
    calendarShow: false,
    date: '',
    maxDate: Date.parse(date),
    minDate: new Date("2000,01,01").getTime(),
    currentDate: new Date().getTime(),
    rotateIcon: false,
    detailed: false,
    list: [],
    detailedData: {},
    explain: false,
    explain_html: "",
    pickStatusShow: false,
    statusText: "全部",
    order_by_status: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    page: 1,
    current_page: 1,
    last_page: 1,

    isLoadMore: true, //判断是否要加载更多的标志
    imgPath: app.globalData.imgPath,
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //跳转至会员权益
  gotoMemberGrade() {
    if(this.data.member.level && this.data.member.level.is_open == 1){
      wx.navigateTo({
        url: "/packageB/member/MemberGradeList/MemberGradeList",
      });
    }
  },
  getDesc() {
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.get-description");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            explain_html: res.data.description
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.get-statistics");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            "member.src": res.data.avatar,
            "member.nickname": res.data.nickname,
            "member.member_id": res.data.member_id,
            "member.level_name": res.data.level_name,
            "member.validity_value": res.data.expire_date,
            "member.cumulative_share_amoun": res.data.total_money,
            "member.last_period": res.data.money,
            "member.frequency": res.data.total_count,
            "member.bonus_show":res.data.bonus_show,
            "member.bonus_num":res.data.bonus_num,
            "member.detail_show":res.data.detail_show,
            "member.level":res.data.level
          });
          let pluginName = wx.getStorageSync('yz_basic_info')?.plugin_setting?.share_partner?.diy_name||'共享合伙人';
          wx.setNavigationBarTitle({
            title: pluginName
          });
          this.setData({
            pluginName
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toLink(){
    wx.navigateTo({
      url: '/packageF/sharedPartner_bonus/sharedPartner_bonus',
    })
  },  
  explainIsShow() {
    this.setData({
      explain: !this.data.explain
    })
  },
  sorting() {
    if(this.data.active === '') this.data.active = false;
    this.setData({
      active: !this.data.active,
      page: 1
    });
    this.getList();
  },

  onClose() {
    this.setData({
      calendarShow: false
    });
  },
  delDate() {
    this.setData({
      date: "",
      page: 1
    });
    this.getList();
  },
  tapCalendar() {
    this.setData({
      calendarShow: true
    });
  },
  onConfirm(e) {
    this.setData({
      calendarShow: false,
      date: urilt.formatTimeTwo((e.detail / 1000), "Y-M-D"),
      page: 1
    });
    this.getList();
  },
  tapRotateIcon() {
    this.setData({
      rotateIcon: !this.data.rotateIcon,
      pickStatusShow: !this.data.pickStatusShow
    });
  },
  tapPickStatus({
    currentTarget: {
      dataset: {
        status,
        text
      }
    }
  }) {
    this.setData({
      statusText: text,
      rotateIcon: false,
      pickStatusShow: false,
      order_by_status: status,
      page: 1
    })
    this.getList();
  },
  detailedIsShowClose() {
    this.setData({
      detailed: false
    })
  },
  detailedIsShow({
    currentTarget: {
      dataset: {
        item
      }
    }
  }) {
    //detailId 分成id
    let detailId = item.id;
    this.getDetail(detailId)
  },
  receive(e) {
    //detailId 分成id
    let detailId = e.currentTarget.dataset.item.id;
    this.getDraw(detailId);
  },
  getDraw(kid) {
    let that = this;
    let json = {
      dividend_id: kid,
    }
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.draw");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (res.data.status == 1) {
            wx.showModal({
              title: '领取成功',
              content: '恭喜您已经成功领取奖励',
              success(res) {
                that.setData({
                  page: 1,
                  current_page: 1,
                  last_page: 1
                })
                that.getList();
              }
            })
          } else if (res.data.status == 2) {
            this.getAdv(kid);
          } else {
            return
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getAdv(kid) {
    let urlStr = app.getNetAddresss("plugin.flyers-advertise.frontend.mini.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let mini_form_id = res.data.mini_form_id || '';
          let adv_id = res.data.adv_list[0].adv_id;
          wx.redirectTo({
            url: "/packageI/AdBright/AdBrightPage/AdBrightPage?adv_id=" + adv_id + '&wxad=' + mini_form_id + '&Partner=Partner' + '&pid=' + kid,
            fail: function (err) {
              console.log(err)
              wx.showToast({
                title: '请使用多功能版本小程序',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getDetail(kid) {
    let json = {
      id: kid,
      page: this.data.page
    }
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.get-detail");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            detailedData: res.data,
            detailed: true
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
    this.getDesc();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  getMore() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
    let json = {
      status: this.data.order_by_status,
      time: this.data.date,
      page: this.data.page + 1
    }
    if(this.data.active !== '') json.order_by= this.data.active ? 'asc' : 'desc';
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.get-list");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        this.data.isLoadMore = true;
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            page: this.data.page + 1,
            list: this.data.list.concat(res.data.data),
            current_page: res.data.current_page,
            last_page: res.data.last_page
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        this.data.isLoadMore = true;
        console.log(res);
      }
    });
  }
  },
  getList() {
    let json = {
      status: this.data.order_by_status,
      time: this.data.date,
      page: this.data.page
    }
    if(this.data.active !== '') json.order_by= this.data.active ? 'desc' : 'asc';
    let urlStr = app.getNetAddresss("plugin.share-partner.frontend.dividend.get-list");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            list: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})